package cn.helper;

import android.annotation.SuppressLint;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import com.unity3d.player.UnityPlayer;

public class CopyAndPaste {
  @SuppressLint({"HandlerLeak"})
  public static void copy(final String text) {
    (new Thread(new Runnable() {
          public void run() {
            Looper.prepare();
            Handler m_handler = new Handler() {
                public void handleMessage(Message msg) {
                  ClipboardManager clipboard = (ClipboardManager)UnityPlayer.currentActivity.getSystemService("clipboard");
                  ClipData textCd = ClipData.newPlainText("data", text);
                  clipboard.setPrimaryClip(textCd);
                }
              };
            Message message = new Message();
            m_handler.sendMessage(message);
            Looper.loop();
          }
        })).start();
  }
  
  public static String getPaste() {
    try {
      ClipboardManager cm = (ClipboardManager)UnityPlayer.currentActivity.getSystemService("clipboard");
      ClipData clipData = cm.getPrimaryClip();
      ClipData.Item item = clipData.getItemAt(0);
      return item.getText().toString();
    } catch (Exception e) {
      Log.i("BToolkit", e.getMessage());
      return "";
    } 
  }
}
